﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CNLTHD_GoogleMapAPI_DTO;
using System.Data.OleDb;

namespace CNLTHD_GoogleMapAPI_DAO
{
    public class DiaDiemDAO
    {
        public static List<DiaDiem> LayDanhSachDiaDiem()
        {
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return null;
            }

            string truyVan = @"SELECT d.MaDiaDiem, d.KinhDo, d.ViDo, d.GhiChu,
									l.MaLoaiDiaDiem, l.TenLoaiDiaDiem, n.MaNguoiDung, n.TenTaiKhoan, n.MatKhau, n.HoTen,
									d.TenDiaDiem 
								FROM Dia_Diem d, LOAI_DIA_DIEM l, NGUOI_DUNG n
								WHERE d.MaLoaiDiaDiem = l.MaLoaiDiaDiem AND d.MaNguoiDung = n.MaNguoiDung AND d.TinhTrang<>0 ";

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);

            List<DiaDiem> danhSach = new List<DiaDiem>();
            try
            {
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    int maDiaDiem = reader.GetInt32(0);

                    double kinhDo = !reader.IsDBNull(1) ? reader.GetDouble(1) : -1;
                    double viDo = !reader.IsDBNull(2) ? reader.GetDouble(2) : -1;
                    string ghiChu = !reader.IsDBNull(3) ? reader.GetString(3) : "Không có ghi chú";
                    string tenDiaDiem = !reader.IsDBNull(10) ? reader.GetString(10) : "Địa điểm chưa có tên";

                    int maLoaiDiaDiem = reader.GetInt32(4);
                    string tenLoaiDiaDiem = !reader.IsDBNull(5) ? reader.GetString(5) : "Chưa có tên";

                    int maNguoiDung = reader.GetInt32(6);
                    string tenTaiKhoan = !reader.IsDBNull(7) ? reader.GetString(7) : "";
                    string matKhau = !reader.IsDBNull(8) ? reader.GetString(8) : "";
                    string hoTen = !reader.IsDBNull(9) ? reader.GetString(9) : "Chưa đặt tên";

                    LoaiDiaDiem ldd = new LoaiDiaDiem { TenLoaiDiaDiem = tenLoaiDiaDiem, MaLoaiDiaDiem = maLoaiDiaDiem };
                    NguoiDung nd = new NguoiDung { TenTaiKhoan = tenTaiKhoan, MaNguoiDung = maNguoiDung, HoTen = hoTen, MatKhau = matKhau };
                    DiaDiem dd = new DiaDiem
                    {
                        MaDiaDiem = maDiaDiem,
                        KinhDo = kinhDo,
                        ViDo = viDo,
                        GhiChu = ghiChu,
                        NguoiDung = nd,
                        LoaiDiaDiem = ldd,
                        TenDiaDiem = tenDiaDiem
                    };

                    danhSach.Add(dd);
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Lấy danh sách địa điểm bị lỗi");
                ketNoi.Close();
                return null;
            }
            finally
            {
            }

            ketNoi.Close();
            return danhSach;
        }

        public static DiaDiem LayDiaDiem(int maDiaDiem)
        {
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return null;
            }

            string truyVan = @"SELECT d.MaDiaDiem, d.KinhDo, d.ViDo, d.GhiChu,
									l.MaLoaiDiaDiem, l.TenLoaiDiaDiem, n.MaNguoiDung, n.TenTaiKhoan, n.MatKhau, n.HoTen,
									d.TenDiaDiem 
								FROM Dia_Diem d, LOAI_DIA_DIEM l, NGUOI_DUNG n
								WHERE d.MaLoaiDiaDiem = l.MaLoaiDiaDiem AND d.MaNguoiDung = n.MaNguoiDung
								AND d.MaDiaDiem = @MaDiaDiem AND d.TinhTrang<>0";
            OleDbParameter thamSoMaDiaDiem = new OleDbParameter("@MaDiaDiem", maDiaDiem);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add(thamSoMaDiaDiem);

            DiaDiem dd = null;
            try
            {
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    //int maDiaDiem = reader.GetInt32(0);

                    double kinhDo = !reader.IsDBNull(1) ? reader.GetDouble(1) : -1;
                    double viDo = !reader.IsDBNull(2) ? reader.GetDouble(2) : -1;
                    string ghiChu = !reader.IsDBNull(3) ? reader.GetString(3) : "Không có ghi chú";
                    string tenDiaDiem = !reader.IsDBNull(10) ? reader.GetString(10) : "Địa điểm chưa có tên";

                    int maLoaiDiaDiem = reader.GetInt32(4);
                    string tenLoaiDiaDiem = !reader.IsDBNull(5) ? reader.GetString(5) : "Chưa có tên";

                    int maNguoiDung = reader.GetInt32(6);
                    string tenTaiKhoan = !reader.IsDBNull(7) ? reader.GetString(7) : "";
                    string matKhau = !reader.IsDBNull(8) ? reader.GetString(8) : "";
                    string hoTen = !reader.IsDBNull(9) ? reader.GetString(9) : "Chưa đặt tên";

                    LoaiDiaDiem ldd = new LoaiDiaDiem { TenLoaiDiaDiem = tenLoaiDiaDiem, MaLoaiDiaDiem = maLoaiDiaDiem };
                    NguoiDung nd = new NguoiDung { TenTaiKhoan = tenTaiKhoan, MaNguoiDung = maNguoiDung, HoTen = hoTen, MatKhau = matKhau };
                    dd = new DiaDiem { MaDiaDiem = maDiaDiem, KinhDo = kinhDo, ViDo = viDo, GhiChu = ghiChu, NguoiDung = nd, LoaiDiaDiem = ldd, TenDiaDiem = tenDiaDiem };
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Lấy danh sách địa điểm bị lỗi");
                ketNoi.Close();
                return null;
            }
            finally
            {
            }

            ketNoi.Close();
            return dd;

        }

        public static List<DiaDiem> LayDanhSachDiaDiem(int maNguoiDung, int maLoaiDD)
        {
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return null;
            }

            string truyVan = @"SELECT d.MaDiaDiem, d.KinhDo, d.ViDo, d.GhiChu,
									l.MaLoaiDiaDiem, l.TenLoaiDiaDiem, n.MaNguoiDung, n.TenTaiKhoan, n.MatKhau, n.HoTen,
									d.TenDiaDiem 
								FROM Dia_Diem d, LOAI_DIA_DIEM l, NGUOI_DUNG n
								WHERE d.MaLoaiDiaDiem = l.MaLoaiDiaDiem AND d.MaNguoiDung = n.MaNguoiDung
								AND d.MaNguoiDung = @MaNguoiDung AND d.MaLoaiDiaDiem = @MaLoaiDiaDiem";
            OleDbParameter thamSoMaNguoiDung = new OleDbParameter("@MaNguoiDung", maNguoiDung);


            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add(thamSoMaNguoiDung);
            thamSoMaNguoiDung = new OleDbParameter("@MaLoaiDiaDiem", maLoaiDD);
            cmd.Parameters.Add(thamSoMaNguoiDung);
            List<DiaDiem> danhSach = new List<DiaDiem>();
            try
            {
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    int maDiaDiem = reader.GetInt32(0);

                    double kinhDo = !reader.IsDBNull(1) ? reader.GetDouble(1) : -1;
                    double viDo = !reader.IsDBNull(2) ? reader.GetDouble(2) : -1;
                    string ghiChu = !reader.IsDBNull(3) ? reader.GetString(3) : "Không có ghi chú";
                    string tenDiaDiem = !reader.IsDBNull(10) ? reader.GetString(10) : "Địa điểm chưa có tên";

                    int maLoaiDiaDiem = reader.GetInt32(4);
                    string tenLoaiDiaDiem = !reader.IsDBNull(5) ? reader.GetString(5) : "Chưa có tên";

                    maNguoiDung = reader.GetInt32(6);
                    string tenTaiKhoan = !reader.IsDBNull(7) ? reader.GetString(7) : "";
                    string matKhau = !reader.IsDBNull(8) ? reader.GetString(8) : "";
                    string hoTen = !reader.IsDBNull(9) ? reader.GetString(9) : "Chưa đặt tên";

                    LoaiDiaDiem ldd = new LoaiDiaDiem { TenLoaiDiaDiem = tenLoaiDiaDiem, MaLoaiDiaDiem = maLoaiDiaDiem };
                    NguoiDung nd = new NguoiDung { TenTaiKhoan = tenTaiKhoan, MaNguoiDung = maNguoiDung, HoTen = hoTen, MatKhau = matKhau };
                    DiaDiem dd = new DiaDiem
                    {
                        MaDiaDiem = maDiaDiem,
                        KinhDo = kinhDo,
                        ViDo = viDo,
                        GhiChu = ghiChu,
                        NguoiDung = nd,
                        LoaiDiaDiem = ldd,
                        TenDiaDiem = tenDiaDiem
                    };
                    danhSach.Add(dd);
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Lấy danh sách địa điểm bị lỗi");
                ketNoi.Close();
                return null;
            }
            finally
            {
            }

            ketNoi.Close();
            return danhSach;
            return null;
        }

        public static List<DiaDiem> LayDiaDiem(string tenDiaDiem)
        {
            if (!KiemTraDoiTuongHopLe.KiemTraTenDiaDiem(tenDiaDiem))
            {
                throw new Exception("Tên địa điểm quá ngắn( < 3 kí tự.");
                return null;
            }
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return null;
            }

            string truyVan = @"SELECT d.MaDiaDiem, d.KinhDo, d.ViDo, d.GhiChu,
									l.MaLoaiDiaDiem, l.TenLoaiDiaDiem, n.MaNguoiDung, n.TenTaiKhoan, n.MatKhau, n.HoTen,
									d.TenDiaDiem 
								FROM Dia_Diem d, LOAI_DIA_DIEM l, NGUOI_DUNG n
								WHERE d.MaLoaiDiaDiem = l.MaLoaiDiaDiem AND d.MaNguoiDung = n.MaNguoiDung
								AND l.TenDiaDiem = @TenDiaDiem AND d.TinhTrang<>0";
            OleDbParameter thamSoMaDiaDiem = new OleDbParameter("@TenDiaDiem", tenDiaDiem);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add(thamSoMaDiaDiem);

            List<DiaDiem> danhSach = new List<DiaDiem>();
            try
            {
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    int maDiaDiem = reader.GetInt32(0);

                    double kinhDo = !reader.IsDBNull(1) ? reader.GetDouble(1) : -1;
                    double viDo = !reader.IsDBNull(2) ? reader.GetDouble(2) : -1;
                    string ghiChu = !reader.IsDBNull(3) ? reader.GetString(3) : "Không có ghi chú";
                    //string tenDiaDiem = !reader.IsDBNull(10) ? reader.GetString(10) : "Địa điểm chưa có tên";

                    int maLoaiDiaDiem = reader.GetInt32(4);
                    string tenLoaiDiaDiem = !reader.IsDBNull(5) ? reader.GetString(5) : "Chưa có tên";

                    int maNguoiDung = reader.GetInt32(6);
                    string tenTaiKhoan = !reader.IsDBNull(7) ? reader.GetString(7) : "";
                    string matKhau = !reader.IsDBNull(8) ? reader.GetString(8) : "";
                    string hoTen = !reader.IsDBNull(9) ? reader.GetString(9) : "Chưa đặt tên";

                    LoaiDiaDiem ldd = new LoaiDiaDiem { TenLoaiDiaDiem = tenLoaiDiaDiem, MaLoaiDiaDiem = maLoaiDiaDiem };
                    NguoiDung nd = new NguoiDung { TenTaiKhoan = tenTaiKhoan, MaNguoiDung = maNguoiDung, HoTen = hoTen, MatKhau = matKhau };
                    DiaDiem dd = new DiaDiem
                    {
                        MaDiaDiem = maDiaDiem,
                        KinhDo = kinhDo,
                        ViDo = viDo,
                        GhiChu = ghiChu,
                        NguoiDung = nd,
                        LoaiDiaDiem = ldd,
                        TenDiaDiem = tenDiaDiem
                    };
                    danhSach.Add(dd);
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Lấy danh sách địa điểm bị lỗi");
                ketNoi.Close();
                return null;
            }
            finally
            {
            }

            ketNoi.Close();
            return danhSach;
        }



        public static List<DiaDiem> LayDanhSachDiaDiem(int maNguoiDung)
        {
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return null;
            }

            string truyVan = @"SELECT d.MaDiaDiem, d.KinhDo, d.ViDo, d.GhiChu,
									l.MaLoaiDiaDiem, l.TenLoaiDiaDiem, n.MaNguoiDung, n.TenTaiKhoan, n.MatKhau, n.HoTen,
									d.TenDiaDiem 
								FROM Dia_Diem d, LOAI_DIA_DIEM l, NGUOI_DUNG n
								WHERE d.MaLoaiDiaDiem = l.MaLoaiDiaDiem AND d.MaNguoiDung = n.MaNguoiDung
								AND d.MaNguoiDung = @MaNguoiDung AND d.TinhTrang<>0";
            OleDbParameter thamSoMaNguoiDung = new OleDbParameter("@MaNguoiDung", maNguoiDung);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add(thamSoMaNguoiDung);

            List<DiaDiem> danhSach = new List<DiaDiem>();
            try
            {
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    int maDiaDiem = reader.GetInt32(0);

                    double kinhDo = !reader.IsDBNull(1) ? reader.GetDouble(1) : -1;
                    double viDo = !reader.IsDBNull(2) ? reader.GetDouble(2) : -1;
                    string ghiChu = !reader.IsDBNull(3) ? reader.GetString(3) : "Không có ghi chú";
                    string tenDiaDiem = !reader.IsDBNull(10) ? reader.GetString(10) : "Địa điểm chưa có tên";

                    int maLoaiDiaDiem = reader.GetInt32(4);
                    string tenLoaiDiaDiem = !reader.IsDBNull(5) ? reader.GetString(5) : "Chưa có tên";

                    maNguoiDung = reader.GetInt32(6);
                    string tenTaiKhoan = !reader.IsDBNull(7) ? reader.GetString(7) : "";
                    string matKhau = !reader.IsDBNull(8) ? reader.GetString(8) : "";
                    string hoTen = !reader.IsDBNull(9) ? reader.GetString(9) : "Chưa đặt tên";

                    LoaiDiaDiem ldd = new LoaiDiaDiem { TenLoaiDiaDiem = tenLoaiDiaDiem, MaLoaiDiaDiem = maLoaiDiaDiem };
                    NguoiDung nd = new NguoiDung { TenTaiKhoan = tenTaiKhoan, MaNguoiDung = maNguoiDung, HoTen = hoTen, MatKhau = matKhau };
                    DiaDiem dd = new DiaDiem
                    {
                        MaDiaDiem = maDiaDiem,
                        KinhDo = kinhDo,
                        ViDo = viDo,
                        GhiChu = ghiChu,
                        NguoiDung = nd,
                        LoaiDiaDiem = ldd,
                        TenDiaDiem = tenDiaDiem
                    };
                    danhSach.Add(dd);
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Lấy danh sách địa điểm bị lỗi");
                ketNoi.Close();
                return null;
            }
            finally
            {
            }

            ketNoi.Close();
            return danhSach;
            return null;
        }

        public static List<DiaDiem> LayDanhSachDiaDiem(string tenTaiKhoan)
        {
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return null;
            }

            string truyVan = @"SELECT d.MaDiaDiem, d.KinhDo, d.ViDo, d.GhiChu,
									l.MaLoaiDiaDiem, l.TenLoaiDiaDiem, n.MaNguoiDung, n.TenTaiKhoan, n.MatKhau, n.HoTen,
									d.TenDiaDiem 
								FROM Dia_Diem d, LOAI_DIA_DIEM l, NGUOI_DUNG n
								WHERE d.MaLoaiDiaDiem = l.MaLoaiDiaDiem AND d.MaNguoiDung = n.MaNguoiDung
								AND n.TenTaiKhoan = @TenTaiKhoan AND d.TinhTrang<>0";
            OleDbParameter thamSoMaNguoiDung = new OleDbParameter("@TenTaiKhoan", tenTaiKhoan);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add(thamSoMaNguoiDung);

            List<DiaDiem> danhSach = new List<DiaDiem>();
            try
            {
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    int maDiaDiem = reader.GetInt32(0);

                    double kinhDo = !reader.IsDBNull(1) ? reader.GetDouble(1) : -1;
                    double viDo = !reader.IsDBNull(2) ? reader.GetDouble(2) : -1;
                    string ghiChu = !reader.IsDBNull(3) ? reader.GetString(3) : "Không có ghi chú";
                    string tenDiaDiem = !reader.IsDBNull(10) ? reader.GetString(10) : "Địa điểm chưa có tên";

                    int maLoaiDiaDiem = reader.GetInt32(4);
                    string tenLoaiDiaDiem = !reader.IsDBNull(5) ? reader.GetString(5) : "Chưa có tên";

                    int maNguoiDung = reader.GetInt32(6);
                    tenTaiKhoan = !reader.IsDBNull(7) ? reader.GetString(7) : "";
                    string matKhau = !reader.IsDBNull(8) ? reader.GetString(8) : "";
                    string hoTen = !reader.IsDBNull(9) ? reader.GetString(9) : "Chưa đặt tên";

                    LoaiDiaDiem ldd = new LoaiDiaDiem { TenLoaiDiaDiem = tenLoaiDiaDiem, MaLoaiDiaDiem = maLoaiDiaDiem };
                    NguoiDung nd = new NguoiDung { TenTaiKhoan = tenTaiKhoan, MaNguoiDung = maNguoiDung, HoTen = hoTen, MatKhau = matKhau };
                    DiaDiem dd = new DiaDiem
                    {
                        MaDiaDiem = maDiaDiem,
                        KinhDo = kinhDo,
                        ViDo = viDo,
                        GhiChu = ghiChu,
                        NguoiDung = nd,
                        LoaiDiaDiem = ldd,
                        TenDiaDiem = tenDiaDiem
                    };
                    danhSach.Add(dd);
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Lấy danh sách địa điểm bị lỗi");
                ketNoi.Close();
                return null;
            }
            finally
            {
            }

            ketNoi.Close();
            return danhSach;
            return null;
        }


        public static List<DiaDiem> LayDanhSachDiaDiem(LoaiDiaDiem loaidd)
        {
            if (!KiemTraDoiTuongHopLe.KiemTraLoaiDiaDiem(loaidd))
            {
                throw new Exception(" Loại địa điểm không đúng, không thể lấy danh sách địa điểm");
                return null;
            }

            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return null;
            }

            string truyVan = @"SELECT d.MaDiaDiem, d.KinhDo, d.ViDo, d.GhiChu,
									l.MaLoaiDiaDiem, l.TenLoaiDiaDiem, n.MaNguoiDung, n.TenTaiKhoan, n.MatKhau, n.HoTen,
									d.TenDiaDiem 
								FROM Dia_Diem d, LOAI_DIA_DIEM l, NGUOI_DUNG n
								WHERE d.MaLoaiDiaDiem = l.MaLoaiDiaDiem AND d.MaNguoiDung = n.MaNguoiDung
								AND d.MaLoaiDiaDiem = @MaLoaiDiaDiem AND d.TinhTrang<>0";
            OleDbParameter thamSoMaLoaiDiaDiem = new OleDbParameter("@MaLoaiDiaDiem", loaidd.MaLoaiDiaDiem);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add(thamSoMaLoaiDiaDiem);

            List<DiaDiem> danhSach = new List<DiaDiem>();
            try
            {
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    int maDiaDiem = reader.GetInt32(0);

                    double kinhDo = !reader.IsDBNull(1) ? reader.GetDouble(1) : -1;
                    double viDo = !reader.IsDBNull(2) ? reader.GetDouble(2) : -1;
                    string ghiChu = !reader.IsDBNull(3) ? reader.GetString(3) : "Không có ghi chú";
                    string tenDiaDiem = !reader.IsDBNull(10) ? reader.GetString(10) : "Địa điểm chưa có tên";

                    int maLoaiDiaDiem = reader.GetInt32(4);
                    string tenLoaiDiaDiem = !reader.IsDBNull(5) ? reader.GetString(5) : "Chưa có tên";

                    int maNguoiDung = reader.GetInt32(6);
                    string tenTaiKhoan = !reader.IsDBNull(7) ? reader.GetString(7) : "";
                    string matKhau = !reader.IsDBNull(8) ? reader.GetString(8) : "";
                    string hoTen = !reader.IsDBNull(9) ? reader.GetString(9) : "Chưa đặt tên";

                    LoaiDiaDiem ldd = new LoaiDiaDiem { TenLoaiDiaDiem = tenLoaiDiaDiem, MaLoaiDiaDiem = maLoaiDiaDiem };
                    NguoiDung nd = new NguoiDung { TenTaiKhoan = tenTaiKhoan, MaNguoiDung = maNguoiDung, HoTen = hoTen, MatKhau = matKhau };
                    DiaDiem dd = new DiaDiem
                    {
                        MaDiaDiem = maDiaDiem,
                        KinhDo = kinhDo,
                        ViDo = viDo,
                        GhiChu = ghiChu,
                        NguoiDung = nd,
                        LoaiDiaDiem = ldd,
                        TenDiaDiem = tenDiaDiem
                    };
                    danhSach.Add(dd);
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Lấy danh sách địa điểm bị lỗi");
                ketNoi.Close();
                return null;
            }
            finally
            {
            }

            ketNoi.Close();
            return danhSach;
            return null;
        }



        public static bool ThemDiaDiem(double kinhDo, double viDo, int maNguoiDung, int maLoaidd, string tenDiaDiem, string ghiChu = "Không có")
        {
            if (!KiemTraDoiTuongHopLe.KiemTraDiaDiem(kinhDo, viDo, maLoaidd, maNguoiDung, tenDiaDiem))
            {
                throw new Exception("Dữ liệu nhập vào không đúng, không thể thêm địa điểm");
                return false;
            }

            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return false;
            }

            if (ghiChu == null)
            {
                ghiChu = "";
            }
            string truyVan = @"INSERT INTO DIA_DIEM(MaLoaiDiaDiem, KinhDo, ViDo, GhiChu, MaNguoiDung, TenDiaDiem, TinhTrang)
								VALUES(@MaLoaiDiaDiem, @KinhDo, @ViDo, @GhiChu, @MaNguoiDung, @TenDiaDiem, '1')";
            OleDbParameter thamSoMaLoaiDiaDiem = new OleDbParameter("@MaLoaiDiaDiem", maLoaidd);
            OleDbParameter thamSoKinhDo = new OleDbParameter("@KinhDo", kinhDo);
            OleDbParameter thamSoViDo = new OleDbParameter("@MaViDo", viDo);
            OleDbParameter thamSoGhiChu = new OleDbParameter("@GhiChu", ghiChu);
            OleDbParameter thamSoMaNguoiDung = new OleDbParameter("@MaNguoiDung", maNguoiDung);
            OleDbParameter thamSoTenDiaDiem = new OleDbParameter("@TenDiaDiem", tenDiaDiem);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add(thamSoMaLoaiDiaDiem);
            cmd.Parameters.Add(thamSoKinhDo);
            cmd.Parameters.Add(thamSoViDo);
            cmd.Parameters.Add(thamSoGhiChu);
            cmd.Parameters.Add(thamSoMaNguoiDung);
            cmd.Parameters.Add(thamSoTenDiaDiem);

            try
            {
                int result = cmd.ExecuteNonQuery();

                if (result >= 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Thêm đia điểm bị lỗi");
                ketNoi.Close();
                return false;
            }
            finally
            {
            }

            ketNoi.Close();

            return true;
        }

        public static bool ThemDiaDiem(double kinhDo, double viDo, NguoiDung nd, LoaiDiaDiem Loaidd, string tenDiaDiem, string ghiChu = "Không có")
        {
            int maNguoiDung = nd.MaNguoiDung;
            int maLoaidd = Loaidd.MaLoaiDiaDiem;

            return ThemDiaDiem(kinhDo, viDo, maNguoiDung, maLoaidd, tenDiaDiem, ghiChu);
        }

        public static bool XoaDiaDiem(int maDiaDiem)
        {
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return false;
            }
            string truyVan = @"DELETE FROM DIA_DIEM
								 WHERE MaDiaDiem = @MaDiaDiem";
            OleDbParameter thamSoMaDiaDiem = new OleDbParameter("@MaDiaDiem", maDiaDiem);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);
            cmd.Parameters.Add((thamSoMaDiaDiem));

            try
            {
                if (ChiaSeDiaDiemDAO.XoaChiaSe(maDiaDiem))
                {
                    int result = cmd.ExecuteNonQuery();

                    if (result >= 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch (System.Exception ex)
            {
                throw new Exception("Xóa đia điểm bị lỗi");
                ketNoi.Close();
                return false;
            }
            finally
            {
            }

            ketNoi.Close();
            return false;
        }

        public static bool XoaDiaDiemAnToan(int maDiaDiem)
        {
            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return false;
            }

            string truyVan = @"UPDATE DIA_DIEM SET 
								TinhTrang = '0'
								WHERE MaDiaDiem = @MaDiaDiem ";

            OleDbParameter thamSoMaDiaDiem = new OleDbParameter("@MaDiaDiem", maDiaDiem);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);


            cmd.Parameters.Add((thamSoMaDiaDiem));

            try
            {
                int result = cmd.ExecuteNonQuery();

                if (result >= 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Xoa dia diem bi loi");
                ketNoi.Close();
                return false;
            }
            finally
            {
            }

            ketNoi.Close();

            return true;
        }


        public static bool CapNhat(int maDiaDiem, double kinhDo, double viDo, int maNguoiDung, int maLoaidd, string tenDiaDiem, string ghiChu)
        {
            if (!KiemTraDoiTuongHopLe.KiemTraDiaDiem(kinhDo, viDo, maNguoiDung, maLoaidd, tenDiaDiem))
            {
                throw new Exception("Dữ liệu nhập vào không đúng, không thể thêm địa điểm");
                return false;
            }

            OleDbConnection ketNoi = ConnectingClass.KetNoi();

            if (ketNoi == null)
            {
                return false;
            }

            if (ghiChu == null)
            {
                ghiChu = "";
            }
            string truyVan = @"UPDATE DIA_DIEM SET 
								MaLoaiDiaDiem = @MaLoaiDiaDiem, KinhDo = @KinhDo, ViDo = @ViDo,
								GhiChu = @GhiChu, MaNguoiDung = @MaNguoiDung, TenDiaDiem =@TenDiaDiem
								WHERE MaDiaDiem = @MaDiaDiem ";
            OleDbParameter thamSoMaLoaiDiaDiem = new OleDbParameter("@MaLoaiDiaDiem", maLoaidd);
            OleDbParameter thamSoKinhDo = new OleDbParameter("@KinhDo", kinhDo);
            OleDbParameter thamSoViDo = new OleDbParameter("@MaViDo", viDo);
            OleDbParameter thamSoGhiChu = new OleDbParameter("@GhiChu", ghiChu);
            OleDbParameter thamSoMaNguoiDung = new OleDbParameter("@MaNguoiDung", maNguoiDung);
            OleDbParameter thamSoTenDiaDiem = new OleDbParameter("@TenDiaDiem", tenDiaDiem);
            OleDbParameter thamSoMaDiaDiem = new OleDbParameter("@MaDiaDiem", maDiaDiem);

            OleDbCommand cmd = new OleDbCommand(truyVan, ketNoi);

            cmd.Parameters.Add(thamSoMaLoaiDiaDiem);
            cmd.Parameters.Add(thamSoKinhDo);
            cmd.Parameters.Add(thamSoViDo);
            cmd.Parameters.Add(thamSoGhiChu);
            cmd.Parameters.Add(thamSoMaNguoiDung);
            cmd.Parameters.Add(thamSoTenDiaDiem);
            cmd.Parameters.Add((thamSoMaDiaDiem));

            try
            {
                int result = cmd.ExecuteNonQuery();

                if (result >= 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception("Thêm đia điểm bị lỗi");
                ketNoi.Close();
                return false;
            }
            finally
            {
            }

            ketNoi.Close();

            return true;
        }
    }
}
